home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / sbp.zoo / sbp_v3.1 / auxil / decode.c < prev    next >
C/C++ Source or Header  |  1992-02-16  |  2KB  |  99 lines

  1. /*$Source: /usr/home/dhesi/booz/RCS/decode.c,v $*/
  2. /*$Id: decode.c,v 1.8 91/07/08 12:06:52 dhesi Exp $*/
  3. /***********************************************************
  4. Adapted from "ar" archiver written by Haruhiko Okumura.
  5. ***********************************************************/
  6.  
  7. #include "booz.h"
  8. #include "zoo.h"
  9. #include "ar.h"
  10. #include "lzh.h"
  11.  
  12. extern int decoded;     /* from huf.c */
  13.  
  14. static int j;  /* remaining bytes to copy */
  15.  
  16. /* must call this before decoding each file */
  17. int decode_start(void)
  18. {
  19.    huf_decode_start();
  20.    j = 0;
  21.    decoded = 0;
  22. }
  23.  
  24. /* decodes up to 'count' chars (but no more than DICSIZ) into supplied
  25. buffer; returns actual count.  */
  26.  
  27. int decode(uint count, uchar buffer[])
  28. {
  29.    static uint i;
  30.    uint r, c;
  31.  
  32.    r = 0;
  33.    while (--j >= 0) {
  34.       buffer[r] = buffer[i];
  35.       i = (i + 1) & (DICSIZ - 1);
  36.       if (++r == count)
  37.          return r;
  38.    }
  39.    for ( ; ; ) {
  40.       c = decode_c();
  41.       if (decoded)
  42.          return r;
  43.       if (c <= UCHAR_MAX) {
  44.          buffer[r] = c;
  45.          if (++r == count)
  46.             return r;
  47.       } else {
  48.          j = c - (UCHAR_MAX + 1 - THRESHOLD);
  49.          i = (r - decode_p() - 1) & (DICSIZ - 1);
  50.          while (--j >= 0) {
  51.             buffer[r] = buffer[i];
  52.             i = (i + 1) & (DICSIZ - 1);
  53.             if (++r == count)
  54.                return r;
  55.          }
  56.       }
  57.    }
  58. }
  59.  
  60. FILE *arcfile;
  61.  
  62. extern char out_buf_adr[];       /* address of buffer */
  63.  
  64. /*
  65. lzh_decode decodes its input and sends it to output.
  66. Should return error status or byte count, but currently
  67. returns 0.
  68. */
  69.  
  70. int lzh_decode(FILE *infile, FILE *outfile,char *fname)
  71. {
  72.    int n;
  73.      extern int decoded;
  74. #ifdef TEXT_MODE
  75.      int first=1;
  76.      extern int text_mode;
  77. #endif
  78.  
  79.      arcfile = infile;             /* stream to be decoded */
  80.  
  81.    decode_start();
  82.    while (!decoded) {
  83.       n = decode((uint) DICSIZ, (uchar *)out_buf_adr); 
  84.       /* n = count of chars decoded */
  85.  
  86. #ifdef TEXT_MODE
  87.      if( text_mode ) {
  88.             if( first && outfile && to_long(out_buf_adr)!=0x03131211 ) {
  89.                 outfile=freopen(fname,"w",outfile);
  90.          }
  91.          first=0;
  92.      }
  93. #endif
  94.  
  95.       fwrite_crc(out_buf_adr, n, outfile);
  96.    }
  97.    return 0;
  98. }
  99.